package com.edu.solr_news.util;
import com.kennycason.kumo.CollisionMode;
import com.kennycason.kumo.WordCloud;
import com.kennycason.kumo.WordFrequency;
import com.kennycason.kumo.bg.RectangleBackground;
import com.kennycason.kumo.font.KumoFont;
import com.kennycason.kumo.font.scale.SqrtFontScalar;
import com.kennycason.kumo.nlp.FrequencyAnalyzer;
import com.kennycason.kumo.nlp.tokenizers.EnglishWordTokenizer;
import com.kennycason.kumo.palette.LinearGradientColorPalette;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;



@Component //词云组件
public class
CiYun {
    //定义过滤虚词
    final  String[] filterword = {"the", "been","a", "s", "an", "that",  "this",  "these",  "those", "i", "me", "my",  "she", "her",  "hers", "his", "he", "him", "his",  "it",  "its",  "we",  "us",  "our",  "ours",   "they",  "them",  "theirs",  "ours", "here",  "there", "in", "into", "to", "for", "by", "on", "at", "with", "upon", "from", "via",  "of", "under",  "above",  "between",  "among",  "as",  "about", "will", "did", "do", "would", "just", "which",  "when",  "self", "are",  "was", "were",  "is", "am",  "have",  "has",  "had",  "and",  "be",  "do",  "did",  "but",  "or",
            "The", "Been","A",  "S","An", "That",  "This",  "These",  "Those", "I", "Me", "My",  "She", "Her",  "Hers", "His", "He", "Him", "His",  "It",  "Its",  "We",  "Us",  "Our",  "Ours",   "They",  "Them",  "Theirs",  "Ours", "Here",  "There", "In", "Into", "To", "For", "By", "On", "At", "With", "Upon", "From", "Via",  "Of", "Under",  "Above",  "Between",  "Among",  "As",  "About", "Will", "Did", "Do",
            "Would", "Just", "Which",  "When",  "Self", "Are",  "Was", "Were",  "Is", "Am",  "Have",  "Has",  "Had",  "And",  "Be",  "Do",  "Did",  "But",  "Or"};


    public String createCiYun(String text,String filePath,String wordnum){
        List<String> lists = new ArrayList<String>();
        String[] words = text.split("[^a-zA-Z]");  //过滤出只含有字母的
        int flag=0; //过滤虚词标记
        for (String word : words) {
                for (String s : filterword) {
                    if (s.equals(word)|| word.length() == 0){
                        flag=1;
                        break;
                    }
                }
                if (flag==0) lists.add(word);
                flag=0;
        }
        //创建分词对象
        FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer();
        //设置分词返回数量
        frequencyAnalyzer.setWordFrequenciesToReturn(Integer.valueOf(wordnum));
        //设置最小分词长度
        frequencyAnalyzer.setMinWordLength(2);
        //设置分词器
        frequencyAnalyzer.setWordTokenizer(new EnglishWordTokenizer());
        List<WordFrequency> wordFrequencies = frequencyAnalyzer.load(lists);
        //处理中文乱码
        java.awt.Font font = new java.awt.Font("STSong-Light", 2, 18);
        //图片分辨率
        Dimension dimension = new Dimension(1500, 960);
        //创建词云对象
        WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT);
        //设置边缘留空
        wordCloud.setPadding(2);
        wordCloud.setFontScalar(new SqrtFontScalar(60, 60));
        //设置词云显示的三种颜色，越靠前设置表示词频越高的词语的颜色
        wordCloud.setColorPalette(new LinearGradientColorPalette(Color.RED, Color.BLUE, Color.GREEN, 30, 30));

        wordCloud.setKumoFont(new KumoFont(font));
        wordCloud.setBackgroundColor(new Color(255, 255, 255));
        //设置词云图形
        wordCloud.setBackground(new RectangleBackground(dimension));
        wordCloud.build(wordFrequencies);
        wordCloud.writeToFile(filePath);

        return null;
    }


}
